package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {
    private MainPanel mainPanel;
    private JButton next;
    private JButton back;
    int i = 0;

    public MainFrame() {
        this.setSize(700, 200);
        this.setLocationRelativeTo(null);
        this.setTitle("'破防了小游戏'安装程序");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addContent();
        this.setVisible(true);
    }

    private void addContent() {
        this.setLayout(null);
        this.mainPanel = new MainPanel(this);
        this.next = new JButton();
        this.back = new JButton();

        this.mainPanel.setBounds(10, 10, 680, 120);
        this.mainPanel.setBackground(Color.black);

        this.next.setBounds(600, 130, 70, 30);
        this.next.setText("NEXT");
        this.next.setEnabled(false);

        this.back.setBounds(500, 130, 70, 30);
        this.back.setText("BACK");
        this.back.setEnabled(false);

        this.next.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
                i++;
                if (i < 4 && i != 0) {
                    back.setEnabled(true);
                }
                if (i==3){
                    next.setEnabled(false);
                }
                System.out.println(i);
                cardLayout.next(mainPanel);
            }
        });

        this.back.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
                i--;
                if (i > 0 && i < 4) {
                    next.setEnabled(true);
                }
                if (i == 0){
                    back.setEnabled(false);
                }
                System.out.println(i);
                cardLayout.previous(mainPanel);
            }
        });

        this.add(this.mainPanel);
        this.add(this.next);
        this.add(this.back);
    }

    public MainPanel getMainPanel() {
        return mainPanel;
    }

    public JButton getNext() {
        return next;
    }

    public JButton getBack() {
        return back;
    }
}
